﻿using Microsoft.AspNetCore.Mvc;
using SIMS.Shared.DTO;
using SIMS.WebApi.Models;
using SIMS.WebApi.Services.Roles;

namespace SIMS.WebApi.Controllers
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController  : ControllerBase
    {
        private readonly ILogger<RoleController> logger;

        private readonly IRoleAppService _service;

        public RoleController(ILogger<RoleController> logger, IRoleAppService service)
        {
            this.logger = logger;
            this._service = service;
        }

        /// <summary>
        /// 新增role
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRole(RoleDTO role)
        {
            var data = _service.AddRole(role);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 删除Role
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteRole(int id)
        {
            var data = _service.DeleteRole(id);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 获取单个role
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRole(int id)
        {
            var data = _service.GetRole(id);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoles(string? roleName, int pageNum, int pageSize)
        {
            var data = _service.GetRoles(roleName, pageNum, pageSize);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 修改Role
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateRole(RoleDTO role)
        {
            var data = _service.UpdateRole(role);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        [HttpGet]
        public IActionResult GetUserRights(int? userId) {
            var data = _service.GetUserRights(userId);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 获取角色菜单列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleMenus(int? roleId)
        {
            var data = _service.GetRoleMenus(roleId);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 设置角色菜单列表
        /// </summary>
        /// <param name="menuIds"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult SetRoleMenus(int roleId, string menuIds)
        {
            var data = _service.SetRoleMenus(roleId, menuIds);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }
    }
}
